package practice.annotaion.spring.entrance;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

//자동으로  빈  등록
@Component("homeController")
public class HomeController {

	private AlarmDevice alarmDevice;
	private Viewer viewer;

	// name 속성에 자동으로 연결할 빈 객체 이름 입력
	@Resource(name = "camera1")
	private Camera camera1;
	@Resource(name = "camera2")
	private Camera camera2;
	@Resource(name = "camera3")
	private Camera camera3;
	private Camera camera4;
	private List<InfraredRaySensor> sensors;

	// 자동 설정 제한 선언
	@Autowired
	@Qualifier("main")
	private Recorder recorder;
	private DisplayStrategy displayStrategy;

	// prepare() 메서드 자동 설정 적용
	@Autowired //bean객체 자동설정
	public void prepare(AlarmDevice alarmDevice, Viewer viewer) {
		this.alarmDevice = alarmDevice;
		this.viewer = viewer;
	}

	// 배열 자동 설정 및 필수 프로퍼티 미설정
	@Autowired(required = false) //required를 사용하지 않겠다는 것false
	@Qualifier("intrusionDetection")
	public void setSensors(List<InfraredRaySensor> sensors) {
		this.sensors = sensors;
		for (InfraredRaySensor sensor : sensors) {
			System.out.println("센서 등록:  " + sensor);
		}
	}

	public void setCamera1(Camera camera1) {
		this.camera1 = camera1;
	}

	public void setCamera2(Camera camera2) {
		this.camera2 = camera2;
	}

	public void setCamera3(Camera camera3) {
		this.camera3 = camera3;
	}

	@Resource(name = "camera4")
	public void setCamera4(Camera camera4) {
		this.camera4 = camera4;
	}

	public void setRecorder(Recorder recorder) {
		this.recorder = recorder;
	}

	@Resource(name = "displayStrategy")
	public void setDisplayStrategy(DisplayStrategy displayStrategy) {
		this.displayStrategy = displayStrategy;
	}

	// 초기화 처리
	@PostConstruct
	public void init() {
		viewer.add(camera1);
		viewer.add(camera2);
		viewer.add(camera3);
		viewer.add(camera4);
	}

	// 자원 반환 및 종료 처리
	@PreDestroy
	public void close() {
	}

	public void checkSensorAndAlarm() {
		for (InfraredRaySensor sensor : sensors) {
			if (sensor.isObjectFounded()) {
				alarmDevice.alarm(sensor.getName());
			}
		}
	}

	public void showCameraImage() {
		viewer.draw();
	}

}
